<%@ page import="util.DBUtil" %>
<%@ page import="java.sql.ResultSet" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2023/10/13
  Time: 11:12
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    /*将用户录入表单传递过来的数据，新增一条记录到数据库的tb_user表中*/
    //1.获得表单传递过来的数据
    String uname =request.getParameter("username");
    //2.设置要执行的sql语句
    String sql ="insert into tb_user(username,password) values('"+uname+"','123')";
    //3.创建DButil对象
    DBUtil dbUtil =new DBUtil();
    //检测用户名是否已经存在
    String sql2="select * from tb_user where username='"+uname+" '";
    ResultSet rs =dbUtil.query(sql2);
    if (rs.next()){
        out.print("用户名已经存在，增加失败！");
        return;
    }else{
        //4.执行sql
        int n =dbUtil.update(sql);
        //5.对结果进行处理
        if (n>0)
            response.sendRedirect("user.jsp");
        else
            out.print("用户新增失败！");
    }

%>
<html>
<head>
    <title>Title</title>
</head>
<body>

</body>
</html>
